package cn.xinfei.xdecision.data.core.util;


import cn.xinfei.xdecision.common.model.datax.exception.XDecisionDataException;
import cn.xinfei.xdecision.common.model.datax.exception.XDecisionDataExceptionType;
import org.apache.commons.lang3.StringUtils;

public class XyfPreConditions {

    public static void checkMobileNumber(String mobileNumber) {
        checkArgument(StringUtils.length(mobileNumber) == 11, XDecisionDataExceptionType.COMMON_ILLEGAL_MOBILE_NUMBER, "手机号格式不正确,mobileNumber=" + mobileNumber);
    }

    public static void checkArgument(boolean expression) {
        checkArgument(expression, XDecisionDataExceptionType.COMMON_ILLEGAL_PARAM);
    }

    public static void checkArgument(boolean expression, Object errorMessage) {
        checkArgument(expression, XDecisionDataExceptionType.COMMON_ILLEGAL_PARAM, errorMessage);
    }

    public static void checkArgument(boolean expression,
                                     String errorMessageTemplate, Object... errorMessageArgs) {
        checkArgument(expression, XDecisionDataExceptionType.COMMON_ILLEGAL_PARAM, errorMessageTemplate, errorMessageArgs);
    }


    public static void checkArgument(boolean expression, XDecisionDataExceptionType exceptionType) {
        if (!expression) {
            throw new XDecisionDataException(exceptionType);
        }
    }

    public static void checkArgument(boolean expression, XDecisionDataExceptionType exceptionType, Object errorMessage) {
        if (!expression) {
            throw new XDecisionDataException(exceptionType, String.valueOf(errorMessage));
        }
    }


    public static void checkArgument(boolean expression, XDecisionDataExceptionType exceptionType,
                                     String errorMessageTemplate, Object... errorMessageArgs) {
        if (!expression) {
            throw new XDecisionDataException(exceptionType,
                    format(errorMessageTemplate, errorMessageArgs));
        }
    }

    public static void checkState(boolean expression) {
        checkArgument(expression, XDecisionDataExceptionType.COMMON_ILLEGAL_STATE);
    }


    public static void checkState(boolean expression,
                                  String errorMessageTemplate, Object... errorMessageArgs) {
        checkArgument(expression, XDecisionDataExceptionType.COMMON_ILLEGAL_STATE, errorMessageTemplate, errorMessageArgs);
    }


    public static <T> T checkNotNull(T reference) {
        if (reference == null) {
            throw new XDecisionDataException(XDecisionDataExceptionType.COMMON_NULL_POINT);
        }
        return reference;
    }

    public static <T> T checkNotNull(T reference, Object errorMessage) {
        if (reference == null) {
            throw new XDecisionDataException(XDecisionDataExceptionType.COMMON_NULL_POINT, String.valueOf(errorMessage));
        }
        return reference;
    }

    public static <T> T checkNotNull(T reference, String errorMessageTemplate,
                                     Object... errorMessageArgs) {
        if (reference == null) {
            throw new XDecisionDataException(XDecisionDataExceptionType.COMMON_NULL_POINT,
                    format(errorMessageTemplate, errorMessageArgs));
        }
        return reference;
    }

    public static <T> T checkNotNull(T reference, XDecisionDataExceptionType exceptionType) {
        if (reference == null) {
            throw new XDecisionDataException(exceptionType);
        }
        return reference;
    }

    public static <T> T checkNotNull(T reference, XDecisionDataExceptionType exceptionType, Object errorMessage) {
        if (reference == null) {
            throw new XDecisionDataException(exceptionType, String.valueOf(errorMessage));
        }
        return reference;
    }


    public static void checkArgument(boolean expression, String msg, XDecisionDataExceptionType exceptionType, Object... errorMessage) {
        if (!expression) {
            throw new XDecisionDataException(exceptionType, msg, errorMessage);
        }
    }


    public static void checkState(boolean expression, Object errorMessage) {
        checkArgument(expression, XDecisionDataExceptionType.COMMON_ILLEGAL_STATE, errorMessage);
    }


    public static void checkState(boolean expression,
                                  String errorMessage, XDecisionDataExceptionType yqgExceptionType, Object... errorMessageArgs) {
        if (!expression) {
            throw new XDecisionDataException(yqgExceptionType, errorMessage, errorMessageArgs);
        }
    }

    public static void checkState(boolean expression, XDecisionDataExceptionType exceptionType) {
        if (!expression) {
            throw new XDecisionDataException(exceptionType);
        }
    }

    public static void checkState(boolean expression, XDecisionDataExceptionType exceptionType, Object errorMessage) {
        if (!expression) {
            throw new XDecisionDataException(exceptionType, String.valueOf(errorMessage));
        }
    }

    public static void checkState(boolean expression, XDecisionDataExceptionType exceptionType,
                                  String errorMessageTemplate, Object... errorMessageArgs) {
        if (!expression) {
            throw new XDecisionDataException(exceptionType,
                    format(errorMessageTemplate, errorMessageArgs));
        }
    }


    public static <T> T checkNotNull(T reference, XDecisionDataExceptionType exceptionType, String errorMessageTemplate,
                                     Object... errorMessageArgs) {
        if (reference == null) {
            throw new XDecisionDataException(exceptionType,
                    format(errorMessageTemplate, errorMessageArgs));
        }
        return reference;
    }

    static String format(String template, Object... args) {
        // start substituting the arguments into the '%s' placeholders
        StringBuilder builder = new StringBuilder(
                template.length() + 16 * args.length);
        int templateStart = 0;
        int i = 0;
        while (i < args.length) {
            int placeholderStart = template.indexOf("%s", templateStart);
            if (placeholderStart == -1) {
                break;
            }
            builder.append(template.substring(templateStart, placeholderStart));
            builder.append(args[i++]);
            templateStart = placeholderStart + 2;
        }
        builder.append(template.substring(templateStart));

        // if we run out of placeholders, append the extra args in square braces
        if (i < args.length) {
            builder.append(" [");
            builder.append(args[i++]);
            while (i < args.length) {
                builder.append(", ");
                builder.append(args[i++]);
            }
            builder.append("]");
        }

        return builder.toString();
    }

}
